package com.arpa.ntocc.basic.api.controller;


import cn.hutool.core.util.StrUtil;
import com.arpa.core.web.Result;
import com.arpa.ntocc.basic.api.service.UserService;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.ntocc.common.common.validator.Save;
import com.arpa.ntocc.common.common.validator.Update;
import com.arpa.ntocc.common.controller.BaseController;
import com.arpa.ntocc.common.domain.dto.PartyAuthorizeDto;
import com.arpa.ntocc.common.domain.dto.UserDto;
import com.arpa.ntocc.common.domain.entity.Shipment;
import com.arpa.ntocc.common.domain.vo.PartyVO;
import com.arpa.ntocc.common.domain.vo.ResourceVO;
import com.arpa.ntocc.common.domain.vo.UserAuthorizeResourceVo;
import com.arpa.ntocc.common.domain.vo.UserVo;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.ntocc.common.security.annotation.NoReSubmit;
import com.arpa.ntocc.common.security.annotation.NoRepeat;
import com.arpa.ntocc.common.service.IShipmentService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


@Api(tags = "账号管理")
@RestController
@RequestMapping(value = "/users", produces = "application/json;charset=UTF-8")
@Log4j2
public class UserController extends BaseController {

    private final UserService userService;

    private final IShipmentService shipmentService;

    public UserController(UserService userService,
                          IShipmentService shipmentService) {
        this.userService = userService;
        this.shipmentService = shipmentService;
    }





    @ApiOperation(value = "账号授权", notes = "账号授权")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
    })
    @NeedLogin
    @NoReSubmit
    @PostMapping("/authorize")
    public Result authorize(@RequestBody @Validated PartyAuthorizeDto dto, HttpServletRequest request) {
        dto.setGroupCode(UserUtil.getBranchCode());
        userService.authorize(dto);
        return Result.ok("添加成功");
    }


    @ApiOperation(value = "获取资源树", notes = "获取资源树")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = UserAuthorizeResourceVo.class))
    @NeedLogin
    @GetMapping(value = "resourcdTree/{code}")
    public Result resource(@PathVariable String code , HttpServletRequest request) {
        UserVo userVo = userService.getUserDetails(UserUtil.getBranchCode(),code);
        List<String> authorizeResourcd = userService.getUserAuthorizeResourcd(code);
        List<ResourceVO> treeData = userService.resourcdTreeData();
        UserAuthorizeResourceVo userAuthorizeResourceVo = new UserAuthorizeResourceVo();
        userAuthorizeResourceVo.setAllResource(treeData);
        userAuthorizeResourceVo.setAuthorizeResource(authorizeResourcd);
        userAuthorizeResourceVo.setUserInfo(userVo);
        return Result.ok().data(userAuthorizeResourceVo);
    }

    @ApiOperation(value = "获取用户组织入驻商列表", notes = "获取用户组织入驻商列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = Shipment.class))
    @NeedLogin
    @GetMapping(value = "/shipments")
    public Result shipments() {
        List<Shipment> shipmentList = shipmentService.list(Wrappers.lambdaQuery(Shipment.class)
                .select(Shipment::getPartyCode,Shipment::getUserName)
                .eq(Shipment::getBranchCode,UserUtil.getBranchCode())
                .eq(Shipment::getAuthStatus,3)
        );
        return Result.ok().data(shipmentList);
    }

    @ApiOperation(value = "获取用户组织仓库列表", notes = "获取用户组织仓库列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = Shipment.class))
    @NeedLogin
    @GetMapping(value = "/warehouses")
    public Result warehouses() {
        List<Map<String,String>> warehouses = userService.getGroupWarehouse();
        return Result.ok().data(warehouses);
    }


}
